package xin.marcher.wind.process.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import xin.marcher.framework.common.exception.BusinessException;
import xin.marcher.framework.common.exception.GlobalCodeEnum;
import xin.marcher.wind.process.dao.TradePlatformProcessDAO;
import xin.marcher.wind.process.dao.TradePlatformProcessNodeDAO;
import xin.marcher.wind.process.domain.entity.TradePlatformProcessDO;
import xin.marcher.wind.process.domain.entity.TradePlatformProcessNodeDO;
import xin.marcher.wind.process.domain.request.TradePlatformProcessRequest;
import xin.marcher.wind.process.domain.vo.TradePlatformProcessVO;
import xin.marcher.wind.process.service.TradePlatformProcessService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * @version 1.0
 */
@Service
public class TradePlatformProcessServiceImpl implements TradePlatformProcessService {

    @Resource
    private TradePlatformProcessDAO tradePlatformProcessDAO;

    @Resource
    private TradePlatformProcessNodeDAO tradePlatformProcessNodeDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TradePlatformProcessVO addProcess(TradePlatformProcessRequest tradePlatformProcessRequest) {
        Assert.notNull(tradePlatformProcessRequest, "新增流程定义入参不能为空");
        Integer type = tradePlatformProcessRequest.getType();
        String name = tradePlatformProcessRequest.getName();
        String showName = tradePlatformProcessRequest.getShowName();

        Assert.notNull(name, "流程定义名称不能为空");
        Assert.notNull(showName, "流程展示名称不能为空");

        if(type == null) {
            type = 1;
        }

        TradePlatformProcessDO tradePlatformProcessDO = new TradePlatformProcessDO();
        tradePlatformProcessDO.setType(type);
        tradePlatformProcessDO.setName(name);
        tradePlatformProcessDO.setShowName(showName);

        tradePlatformProcessDAO.save(tradePlatformProcessDO);

        TradePlatformProcessVO result = new TradePlatformProcessVO();
        result.setType(type);
        result.setName(name);
        result.setShowName(showName);
        return result;
    }

    @Override
    public TradePlatformProcessVO getProcess(Long id) {
        Assert.notNull(id, "流程定义ID不能为空");
        TradePlatformProcessDO tradePlatformProcessDO = tradePlatformProcessDAO.getById(id);
        if(tradePlatformProcessDO == null) {
            return null;
        }
        TradePlatformProcessVO result = new TradePlatformProcessVO();
        result.setType(tradePlatformProcessDO.getType());
        result.setName(tradePlatformProcessDO.getName());
        result.setShowName(tradePlatformProcessDO.getShowName());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TradePlatformProcessVO editProcess(Long id, TradePlatformProcessRequest tradePlatformProcessRequest) {
        Assert.notNull(id, "流程定义ID不能为空");
        Assert.notNull(tradePlatformProcessRequest, "新增流程定义入参不能为空");
        Integer type = tradePlatformProcessRequest.getType();
        String name = tradePlatformProcessRequest.getName();
        String showName = tradePlatformProcessRequest.getShowName();

        TradePlatformProcessDO tradePlatformProcessDO = tradePlatformProcessDAO.getById(id);
        if(tradePlatformProcessDO == null) {
            throw new BusinessException(GlobalCodeEnum.GL_SERVER_ERROR.getRealCode(), "流程定义{}不存在", id.toString());
        }

        if(type != null) {
            tradePlatformProcessDO.setType(type);
        }

        if(StringUtils.isNotEmpty(name)) {
            tradePlatformProcessDO.setName(name);
        }

        if(StringUtils.isNotEmpty(showName)) {
            tradePlatformProcessDO.setShowName(showName);
        }

        tradePlatformProcessDAO.updateById(tradePlatformProcessDO);

        TradePlatformProcessVO result = new TradePlatformProcessVO();
        result.setType(tradePlatformProcessDO.getType());
        result.setName(tradePlatformProcessDO.getName());
        result.setShowName(tradePlatformProcessDO.getShowName());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteProcess(Long id) {
        TradePlatformProcessDO tradePlatformProcessDO = tradePlatformProcessDAO.getById(id);
        if(tradePlatformProcessDO == null) {
            throw new BusinessException(GlobalCodeEnum.GL_SERVER_ERROR.getRealCode(), "流程定义{}不存在", id.toString());
        }

        // 检查是否还有关联的流程节点没删除
        List<TradePlatformProcessNodeDO> processNodeList = tradePlatformProcessNodeDAO.listByProcessId(id);
        if(processNodeList != null && !processNodeList.isEmpty()) {
            throw new BusinessException(GlobalCodeEnum.GL_SERVER_ERROR.getRealCode(), "当前流程定义存在关联的流程节点数据，请先删除流程节点数据");
        }
        return tradePlatformProcessDAO.removeById(id);
    }

}